package com.jy.adscm.operation.pebase.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.mscyb.bean.JqbjfxPartsrepair;
import com.jy.adscm.operation.mscyb.service.JqbjfxPartsrepairService;
import com.jy.adscm.operation.pebase.bean.PeDiseaseType;
import com.jy.adscm.operation.pebase.bean.QueryPeDiseaseType;
import com.jy.adscm.operation.pebase.service.PeDiseaseTypeService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.system.mapper.mysql.JyCodeMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyl
 * @since 2019-04-10
 */
@RestController
@RequestMapping("/adscm/pebase/peDiseaseType")
public class PeDiseaseTypeController {
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @Autowired
    PeDiseaseTypeService  peDiseaseTypeService;
    @RequestMapping("insert")
    public SimpleCoreResponse insert(@RequestBody PeDiseaseType peDiseaseType, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        peDiseaseType.setCreator(token.getUser().getId().toString());
        peDiseaseType.setCreateTime(new Date());
        boolean insert = peDiseaseTypeService.insert(peDiseaseType);
        return new SimpleCoreResponse("200","新增成功",insert);
    }

    @RequestMapping("delete/{id}")
    public SimpleCoreResponse delete(@PathVariable Integer id ,HttpServletRequest request)throws AdscmSystemException {
        boolean result =false;
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        PeDiseaseType peDiseaseType = peDiseaseTypeService.selectById(id);
         if (  peDiseaseType.getCreator().equals(token.getUser().getId().toString())){
             result=  peDiseaseTypeService.deleteById(id);
         }
        return new  SimpleCoreResponse("200","删除成功",result);
    }
    @RequestMapping("update")
    public SimpleCoreResponse update(@RequestBody PeDiseaseType peDiseaseType, HttpServletRequest request) throws AdscmSystemException{
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        peDiseaseType.setCreator(token.getUser().getId().toString());
        peDiseaseType.setCreateTime(new Date());
        Boolean result = peDiseaseTypeService.updateById(peDiseaseType);
        return  new SimpleCoreResponse("200","修改成功",result);
    }
    @RequestMapping("select")
    public SimpleCoreResponse select(@RequestBody QueryPeDiseaseType queryPeDiseaseType, HttpServletRequest request) throws AdscmSystemException{
        Page<PeDiseaseType> page =new Page<>(queryPeDiseaseType.getCurrentPage(),queryPeDiseaseType.getPageSize());
        page= peDiseaseTypeService.selectPeDiseaseType(page,queryPeDiseaseType);
        return  new SimpleCoreResponse("200","查询成功",page);
    }

    @RequestMapping("selectUsers")
    public SimpleCoreResponse selectUsers( HttpServletRequest request) throws AdscmSystemException{
      List<JyUser> result= peDiseaseTypeService.selectUsers();
        return  new SimpleCoreResponse("200","查询成功",result);
    }
}

